package com.example.costumview.customview2;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ListView;

public class ListViewEx2 extends ListView {

    private HorizontalScrollViewEx2 mHorizontalScrollViewEx2;
    //分别记录上次滑动的坐标
    private int mLastX=0;
    private int mLastY=0;
    public ListViewEx2(Context context) {
        this(context,null);
    }

    public ListViewEx2(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ListViewEx2(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    public void setHorizontalScrollViewEx2(HorizontalScrollViewEx2 horizontalScrollViewEx2){
        this.mHorizontalScrollViewEx2=horizontalScrollViewEx2;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        int x= (int) ev.getX();
        int y= (int) ev.getY();
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                mHorizontalScrollViewEx2.requestDisallowInterceptTouchEvent(true);
                Log.e("TAG", "子布局的分发:requestDisallowInterceptTouchEvent:true" );
                break;
            case MotionEvent.ACTION_MOVE:
                int deltaX=x-mLastX;
                int deltaY=y-mLastY;
                if (Math.abs(deltaX)>Math.abs(deltaY)){
                    mHorizontalScrollViewEx2.requestDisallowInterceptTouchEvent(false);//只有调用false，父布局才能继续拦截所需的事件
                    Log.e("TAG", "子布局的分发:requestDisallowInterceptTouchEvent:false " );
                }
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        mLastX=x;
        mLastY=y;
        return super.dispatchTouchEvent(ev);
    }
}
